"""
# -*- coding: utf-8 -*-

Created on 2021/5/25 11:30

@File: query_database.py
@Author: LT
@Description: Use of this module
"""
import pymysql
from pymysql.cursors import DictCursor      # 为的是fetchall查询结果返回字典，而不是元组
from .ping_database import PingDatabase


class QueryDatabase(PingDatabase):
    def __init__(self):
        PingDatabase.__init__(self)

    def execute_select(self, target_table, target_title, target_content):
        """
        执行查询语句的命令
        :return: boolean or list
        """
        select_statement = """select * from {} where {} like '{}'""".format(target_table, target_title, target_content)
        if self.ping_result:                                        # 只有在数据库连接成功的情况下才能进行查询
            db = pymysql.connect(**self.database_connect_kwargs)             # 建立新的数据库连接
            cur = db.cursor(DictCursor)                             # 使得游标的查询结果返回字典
            try:
                cur.execute(select_statement)
                res = cur.fetchall()
            except:
                return False
            else:
                return res
            finally:
                db.close()


if __name__ == '__main__':
    exp = QueryDatabase()
    res = exp.execute_select('t_user', 'shortName', '瑞安市盘龙鞋业有限公司2——瑞安市帝艺鞋业有限公司（租户）')
    if len(res) > 0:
        print(res)
